package utilities;


import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;

import servers.Dragon;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;


public class WebCommandEngine extends Engine {

	private static boolean active = true;
	private Recognizer recognizer;
	public WebCommandEngine()
	{
	}
	
	public WebCommandEngine(String name, BufferedWriter log, List actionList,
			Object configData, int sleeps, EngineCommander commander) {		
		super(name, log, actionList, configData, sleeps, commander);
	}
	
	public void queueCommand(String command)
	{
		servers.Dragon.commandDelegator(command);
	}
	public void close() 
	{
		this.active=false;

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//recognizer.deallocate();
		
		try {
			log.write(" stopped\n");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		try {
			log.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	public void run()
	{
		BufferedReader in = null;
		try
		{
	    
			log.write("loaded config\n"); 
	    	    
		  while(active) {
	
		    boolean repeat =false;
		     do 
		     {		    	 
				try
				{
			    	URL yahoo = new URL(this.commander.getAppEngineDir());
			        URLConnection yc = yahoo.openConnection();
			        yc.setUseCaches(false);
			        in = new BufferedReader(
			                                new InputStreamReader(
			                                yc.getInputStream()));
			        
		 	        String inputLine="";
		 	        repeat=false;
		 	        
	 	     	    if ((inputLine = in.readLine()) != null) 
	 	     	    {
	 	     	    	if(inputLine.indexOf("No command")>-1) break;
	 	     	    	/*
	 	     	    	//PARSE RECEIVT FOR MORE....
				         if(inputLine.indexOf("more")>-1)
				         {
				        	 inputLine=inputLine.replace("more","");
				        	 repeat=true;
				         }
		     	    	if(inputLine.indexOf("No command")<0)
		     	    	{
		     	    	*/				         
		     	    	System.out.println("Immediate command: " + inputLine);
		     	    	Dragon.logToServer("running: " + inputLine);
		     	    	this.queueCommand(inputLine);	        	
		     	    	
	 	     	    }	
			        in.close();
		        
		     }catch(Exception webex)
		     {
		    	try{
		    		in.close();
		    	}
		    	catch(Exception na){}
		     }
		        
		     } while(repeat);
		    	
	    	Thread.sleep(sleeps);
	    }
			
		}catch (Exception e)
		{
			try {
				
				//log.write("Thread bombed " + e.getMessage() + "\n");
				//e.printStackTrace(new PrintWriter(log));
				e.printStackTrace();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	
}
